clear all
set printcolor gs1, permanently
set more off

cd ""
global dirout ""
global dirobs ""
global tourism ""

use "launch_dates_clean.dta", clear
gen intro_date=mdy(intro_month, intro_day, intro_year)
keep city same intro_date
rename city city_name
set obs 45
replace city_name = "Queretaro" in 44
replace same = 0 in 44
replace city_name = "Estado de Mexico" in 45
replace same = 0 in 45
replace intro_date=20781 in 45
save "TEMP_launch_dates.dta", replace

import delimited "obs_data2.csv", encoding(ISO-8859-1)clear
drop if city=="Veracruz"| city=="Riviera Maya" | city=="Pachuca" | city=="Durango" | city=="Colima"
merge m:1 city_name using "TEMP_launch_dates.dta", nogenerate
erase "TEMP_launch_dates.dta"

split datestr, p("-")
destring datestr*, replace
rename datestr1 year 
rename datestr2 month
rename datestr3 day
gen all_dates=mdy(month, day, year)

gen dow=dow(all_dates) 
replace dow=7 if dow==0
replace dow=dow-1
gen week = all_dates - dow

gen week_intro_date=week if intro_date==all_dates
drop intro_date
bysort city_name: egen intro_date=max(week_intro_date)
format intro_date week %td 
replace num_violent_trips=0 if num_violent_trips==.

rename city_name city
collapse (sum) num* sum*, by(city week intro_date)

drop if city=="Mexico City"
append using df_weekly.dta
append using edo_mex_weekly.dta

gen month=month(week)
gen year=year(week)

merge m:1 city year month using ciudad_estado_mensual_short
drop if _merge==2
drop _merge

merge 1:1 city week using precipitation_weekly_wEDOMEX.dta
drop if _merge==2
drop _merge

merge m:1 city week using mexico_surge_weekly.dta
drop if _merge==2
drop _merge

replace intro_date=20779 if city=="Estado de Mexico"

sort city week
by city: gen trend=_n

drop if sum_fare==0
keep if week>=20548
keep if week<21157 

egen city_id=group(city)
format week %td
tsset city_id week
gen time_to_event=(week-intro_date)/7
gen EVENT=(time_to_event==0)

bysort city_id: egen min_time_to_event=min(time_to_event)
drop if intro_date==. & city!="Mexico City" & city!="Queretaro"

drop if min_time_to_event>-8 & city!="Mexico City" & city!="Queretaro"
drop min_time_to_event
drop if time_to_event==. & city!="Mexico City" & city!="Queretaro"
bysort city_id: egen max_time_to_event=max(time_to_event)

tab time_to_event, g(tte_)
forvalues i=1/125  {
replace tte_`i'=0 if city=="Mexico City" | city=="Queretaro"
}
tab city_id, g(c_)

merge m:1 city year month using "$tourism/tourism.dta"
drop if _merge==2
drop _merge
gen ln_tourism = ln(tourists_foreigners)

gen ln_num_driver_canceled=ln(num_driver_canceled)
gen ln_sum_fare=ln(sum_fare)
gen ln_sum_miles=ln(sum_miles)
gen cancel_rate=num_driver_canceled/num_trips
gen price=ln(sum_fare/sum_miles)
gen miles_per_trip = ln(sum_miles/num_trips)
replace inc_pc=ln(inc_pc)
gen ln_num_trips=ln(num_trips)
gen num_violent_trips_rate=(num_violent_trips/num_trips)*10000
sort city_id week
egen time=group(week)
gen cancel_rate_rider=num_rider_canceled/num_trips

tab time, g(time_)
tsset city_id time
gen trend2=trend*trend

xtscc ln_num_trips tte_1-tte_46 tte_48-tte_125 time_2-time_87  e_rate inc_pc  trend trend2  precipitation  ln_tourism, fe
preserve
	matrix results = [e(b)',vecdiag(e(V))']
		svmat results
		keep results* 
		drop if results1==.
		gen n=_n
		keep if n<125
		gen event=-48
		replace event = even+n
		replace event=event+1 if event>=-1
		set obs 125
		replace results1 = 0 in 125
		replace results2 = 0 in 125
		replace event = -1 in 125
		drop n
		sort event
		rename results1 variable
		cap drop c1 c2
		gen c1=variable + 1.96*sqrt(results2)
		gen c2=variable - 1.96*sqrt(results2)

		#delimit
		twoway 
		(rarea c1 c2 event if event>=-35 & event<=70, pstyle(ci) color(gs15))
		(line variable event if event>=-35 & event<=70, lwidth(thick) lcolor(navy)) 
		, ytitle("Trips (in logs)", height(5)) title(" ")
		xtitle(Weeks) xlabel(-30[10]70) ylabel(-.4[.2]1)  yline(0, lcolor(black))
		legend(off)  xline(0, lcolor(cranberry) lwidth(thick))
		graphregion(color(white)) plotregion(fcolor(white));
		#delimit cr	
		
		graph export "$dirout/Figure2a.eps", replace		
restore	

xtscc ln_sum_fare tte_1-tte_46 tte_48-tte_125 time_2-time_87 e_rate inc_pc  trend trend2 precipitation ln_tourism, fe
		
preserve
	matrix results = [e(b)',vecdiag(e(V))']
		svmat results
		keep results* 
		drop if results1==.
		gen n=_n
		keep if n<125
		gen event=-48
		replace event = even+n
		replace event=event+1 if event>=-1
		set obs 125
		replace results1 = 0 in 125
		replace results2 = 0 in 125
		replace event = -1 in 125
		drop n
		sort event
		rename results1 variable
		cap drop c1 c2
		gen c1=variable + 1.96*sqrt(results2)
		gen c2=variable - 1.96*sqrt(results2)		

		#delimit
		twoway 
		(rarea c1 c2 event if event>=-35 & event<=70, pstyle(ci) color(gs15))
		(line variable event if event>=-35 & event<=70, lwidth(thick) lcolor(navy)) 
		, ytitle("Fares (in logs)", height(5)) title(" ")
		xtitle(Weeks) xlabel(-30[10]70) ylabel(-.4[.2]1)  yline(0, lcolor(black))
		legend(off)  xline(0, lcolor(cranberry) lwidth(thick))
		graphregion(color(white)) plotregion(fcolor(white));
		#delimit cr	
		graph export "$dirout/Figure2b.eps", replace
restore	

xtscc price tte_1-tte_46 tte_48-tte_125 time_2-time_87 e_rate inc_pc  trend trend2  precipitation  ln_tourism, fe
preserve
	matrix results = [e(b)',vecdiag(e(V))']
		svmat results
		keep results* 
		drop if results1==.
		gen n=_n
		keep if n<125
		gen event=-48
		replace event = even+n
		replace event=event+1 if event>=-1
		set obs 125
		replace results1 = 0 in 125
		replace results2 = 0 in 125
		replace event = -1 in 125
		drop n
		sort event
		rename results1 variable
		cap drop c1 c2
		gen c1=variable + 1.96*sqrt(results2)
		gen c2=variable - 1.96*sqrt(results2)

		#delimit
		twoway 
		(rarea c1 c2 event if event>=-35 & event<=70, pstyle(ci) color(gs15))
		(line variable event if event>=-35 & event<=70, lwidth(thick) lcolor(navy)) 
		, ytitle("Fares over Miles (in logs)", height(5)) title(" ")
		xtitle(Weeks) xlabel(-30[10]70) ylabel(-.1[.05]0.1)  yline(0, lcolor(black))
		legend(off)  xline(0, lcolor(cranberry) lwidth(thick))
		graphregion(color(white)) plotregion(fcolor(white));
		#delimit cr	
		graph export "$dirout/Figure3d.eps", replace
restore	

xtscc avg_surge_multiplier tte_10-tte_46 tte_48-tte_125 time_11-time_87 e_rate inc_pc  trend trend2  precipitation ln_tourism, fe			
preserve
	matrix results = [e(b)',vecdiag(e(V))']
		svmat results
		keep results* 
		drop if results1==.
		gen n=_n
		keep if n<125
		gen event=-48
		replace event = even+n
		replace event=event+1 if event>=-1
		set obs 125
		replace results1 = 0 in 125
		replace results2 = 0 in 125
		replace event = -1 in 125
		drop n
		sort event
		rename results1 variable
		cap drop c1 c2
		gen c1=variable + 1.96*sqrt(results2)
		gen c2=variable - 1.96*sqrt(results2)		
		
		#delimit
		twoway 
		(rarea c1 c2 event if event>=-35 & event<=70, pstyle(ci) color(gs15))
		(line variable event if event>=-35 & event<=70, lwidth(thick) lcolor(navy)) 
		, ytitle("Average surge multiplier", height(5)) title(" ")
		xtitle(Weeks) xlabel(-30[10]70) ylabel(-.2[.08].20)  yline(0, lcolor(black))
		legend(off)  xline(0, lcolor(cranberry) lwidth(thick))
		graphregion(color(white)) plotregion(fcolor(white));
		#delimit cr	

		graph export "$dirout/Figure3e.eps", replace
restore	

capture rename 	avg_surge_conditional_on_surged avg_s_c_o_s
xtscc avg_s_c_o_s tte_10-tte_46 tte_48-tte_125 time_11-time_87 e_rate inc_pc  trend trend2  precipitation  ln_tourism, fe	
		
preserve
	matrix results = [e(b)',vecdiag(e(V))']
		svmat results
		keep results* 
		drop if results1==.
		gen n=_n
		keep if n<125
		gen event=-48
		replace event = even+n
		replace event=event+1 if event>=-1
		set obs 125
		replace results1 = 0 in 125
		replace results2 = 0 in 125
		replace event = -1 in 125
		drop n
		sort event
		rename results1 variable
		cap drop c1 c2
		gen c1=variable + 1.96*sqrt(results2)
		gen c2=variable - 1.96*sqrt(results2)		
		
		#delimit
		twoway 
		(rarea c1 c2 event if event>=-35 & event<=70, pstyle(ci) color(gs15))
		(line variable event if event>=-35 & event<=70, lwidth(thick) lcolor(navy)) 
		, ytitle("Average surge multiplier (conditional on surged)", height(5)) title(" ")
		xtitle(Weeks) xlabel(-30[10]70) ylabel(-.2[.1].40)  yline(0, lcolor(black))
		legend(off)  xline(0, lcolor(cranberry) lwidth(thick))
		graphregion(color(white)) plotregion(fcolor(white));
		#delimit cr	
		graph export "$dirout/FigureA1c.eps", replace
restore	


xtscc ln_sum_miles tte_1-tte_46 tte_48-tte_125 time_2-time_87 e_rate inc_pc  trend trend2 precipitation  ln_tourism, fe		
preserve
	matrix results = [e(b)',vecdiag(e(V))']
		svmat results
		keep results* 
		drop if results1==.
		gen n=_n
		keep if n<125
		gen event=-48
		replace event = even+n
		replace event=event+1 if event>=-1
		set obs 125
		replace results1 = 0 in 125
		replace results2 = 0 in 125
		replace event = -1 in 125
		drop n
		sort event
		rename results1 variable
		cap drop c1 c2
		gen c1=variable + 1.96*sqrt(results2)
		gen c2=variable - 1.96*sqrt(results2)

		#delimit
		twoway 
		(rarea c1 c2 event if event>=-35 & event<=70, pstyle(ci) color(gs15))
		(line variable event if event>=-35 & event<=70, lwidth(thick) lcolor(navy)) 
		, ytitle("Miles (in logs)", height(5)) title(" ")
		xtitle(Weeks) xlabel(-30[10]70) ylabel(-.4[.2]1)  yline(0, lcolor(black))
		legend(off)  xline(0, lcolor(cranberry) lwidth(thick))
		graphregion(color(white)) plotregion(fcolor(white));
		#delimit cr	
		graph export "$dirout/FigureA1d.eps", replace
restore	
		
cd ""

use "launch_dates_clean.dta", clear
gen intro_date=mdy(intro_month, intro_day, intro_year)
keep city same intro_date
rename city city_name
set obs 45
replace city_name = "Queretaro" in 44
replace same = 0 in 44
replace city_name = "Estado de Mexico" in 45
replace same = 0 in 45
replace intro_date=20781 in 45
gen dow=dow(intro_date) 
replace dow=7 if dow==0
replace dow=dow-1
gen intro_week = intro_date - dow
format intro_date intro_week %td 
drop intro_date
save "TEMP_launch_dates.dta", replace

import delimited "Driver_SignUps.csv", encoding(ISO-8859-1) clear
split date, p(" ")
split date1, p("-")
destring date1*, replace
rename date11 year 
rename date12 month
rename date13 day
keep city_name su year month day
gen all_dates=mdy(month, day, year)
gen dow=dow(all_dates) 
replace dow=7 if dow==0
replace dow=dow-1
gen week = all_dates - dow
format week all_dates %td 
keep city_name su week
rename city_name city
rename su driver_su
save "Driver_SignUps.dta", replace


import delimited "ETA_and_Supply_hours.csv", encoding(ISO-8859-1) clear
split week, p("-")
destring week*, replace
rename week1 year 
rename week2 month
rename week3 day
keep city_name minutes_per_driver average_eta_seconds year month day
gen all_dates=mdy(month, day, year)
gen dow=dow(all_dates) 
replace dow=7 if dow==0
replace dow=dow-1
gen week = all_dates - dow
format week all_dates %td 
keep city_name minutes_per_driver average_eta_seconds week
rename city_name city
destring average_eta_seconds, replace force
replace average_eta_seconds=average_eta_seconds/60
rename average_eta_seconds avg_eta_min
replace minutes_per_driver=minutes_per_driver/60
rename minutes_per_driver driver_hours
save "ETA_and_Supply_hours.dta", replace

import delimited "$dirobs/obs_data2.csv", encoding(ISO-8859-1)clear
split datestr, p("-")
destring datestr*, replace
rename datestr1 year 
rename datestr2 month
rename datestr3 day
gen all_dates=mdy(month, day, year)
gen dow=dow(all_dates) 
replace dow=7 if dow==0
replace dow=dow-1
gen week = all_dates - dow
format  week %td 
rename city_name city
collapse (sum) num_rider_canceled num_driver_canceled, by(city week)
save "Cancellation.dta", replace


import delimited "SignUps.csv", encoding(ISO-8859-1) clear
split date, p(" ")
split date1, p("-")
destring date1*, replace
rename date11 year 
rename date12 month
rename date13 day
keep city_name su year month day
gen all_dates=mdy(month, day, year)
gen dow=dow(all_dates) 
replace dow=7 if dow==0
replace dow=dow-1
gen week = all_dates - dow
format week all_dates %td 
keep city_name su week
rename city_name city
save "SignUps.dta", replace

import delimited "$dirobs/obs_data2.csv", encoding(ISO-8859-1)clear
split datestr, p("-")
destring datestr*, replace
rename datestr1 year 
rename datestr2 month
rename datestr3 day
gen all_dates=mdy(month, day, year)
gen dow=dow(all_dates) 
replace dow=7 if dow==0
replace dow=dow-1
gen week = all_dates - dow
replace num_violent_trips=0 if num_violent_trips==.
rename city_name city
collapse (sum) num* sum*, by(city week)
save "$dirobs/Sessions_Requests.dta", replace
 

import excel "Active Drivers Mexico.xlsx", sheet("Sheet1") firstrow clear
drop if city=="Veracruz"| city=="Riviera Maya" | city=="Pachuca" | city=="Durango" | city=="Colima"
merge m:1 city_name using "TEMP_launch_dates.dta", nogenerate
erase "TEMP_launch_dates.dta"
gen month=month(week)
gen year=year(week)

bysort city_name: egen intro_date=max(intro_week)
format intro_date week %td 

rename city_name city
merge m:1 city year month using ciudad_estado_mensual_short, keep(match) nogenerate
collapse (sum) active* total* cash* (mean) e_rate inc_pc, by(city week intro_date)

merge 1:1 week city using "SignUps.dta"
drop if _merge==2
drop _merge

merge 1:1 week city using "Driver_SignUps.dta"
drop if _merge==2
drop _merge

merge 1:1 week city using "ETA_and_Supply_hours.dta"
drop if _merge==2
drop _merge

merge 1:1 week city using "Cancellation.dta"
drop if _merge==2
drop _merge

merge 1:1 city week using precipitation_weekly.dta
drop if _merge==2
drop _merge

merge 1:1 city week using "$dirobs/Sessions_Requests.dta"
drop if _merge==2
drop _merge

drop if  total_fares_local==0

sort city week
by city: gen trend=_n
gen trend2=trend*trend

keep if week>=20548
keep if week<21157  

egen city_id=group(city)
format week %td
egen time=group(week)
tsset city_id time
tab time, g(time_)
gen time_to_event=(week-intro_date)/7

gen EVENT=(time_to_event==0)

bysort city_id: egen min_time_to_event=min(time_to_event)
drop if intro_date==. &   city!="Queretaro"
drop if min_time_to_event>-8  & city!="Queretaro"
drop if time_to_event==.   & city!="Queretaro"
bysort city_id: egen max_time_to_event=max(time_to_event)

tab time_to_event, gen(tte_)
forvalues i=1/125  {
replace tte_`i'=0 if  city=="Queretaro"
}
tab city_id, g(c_)

gen year=year(week)
gen month=month(week)

merge m:1 city year month using "$tourism/tourism.dta"
drop if _merge==2
drop _merge
gen ln_tourism = ln(tourists_foreigners)

gen active_riders_drivers=active_riders/active_drivers
gen active_riders_trips=active_riders/total_trips
gen ln_active_riders_trips=ln(active_riders_trips)
gen ln_active_riders_drivers=ln(active_riders)-ln(active_drivers)
gen fares_per_activedriver=(total_fares_local/active_drivers)*(1/19)
gen ln_active_drivers=ln(active_drivers)
gen ln_su=ln(su)
gen ln_driver_su=ln(driver_su)
gen ln_active_riders=ln(active_riders)
gen ln_driver_hours=ln(driver_hours)
gen ln_num_driver_canceled=ln(num_driver_canceled)
gen cancel_rate=num_driver_canceled/total_trips
gen cancel_rate_rider=num_rider_canceled/total_trips
gen ln_fares_hours=ln(total_fares_local/(active_drivers*driver_hours))
gen request_session=num_requests/num_sessions
gen num_violent_trips_rate=(num_violent_trips/num_trips)*10000
gen ln_epaid_trips=ln(total_trips-cash_trips)
gen ln_epaid_fares=ln(total_fares_local-cash_fares_local)

 
xtscc ln_active_riders tte_1-tte_46 tte_48-tte_125 time_2-time_87 e_rate inc_pc trend trend2 precipitation ln_tourism, fe

preserve
	matrix results = [e(b)',vecdiag(e(V))']
		svmat results
		keep results* 
		drop if results1==.
		gen n=_n
		keep if n<125
		gen event=-48
		replace event = even+n
		replace event=event+1 if event>=-1
		set obs 125
		replace results1 = 0 in 125
		replace results2 = 0 in 125
		replace event = -1 in 125
		drop n
		sort event
		rename results1 variable
		cap drop c1 c2
		gen c1=variable + 1.96*sqrt(results2)
		gen c2=variable - 1.96*sqrt(results2)

		#delimit
		twoway 
		(rarea c1 c2 event if event>=-35 & event<=70, pstyle(ci) color(gs15))
		(line variable event if event>=-35 & event<=70, lwidth(thick) lcolor(navy)) 
		, ytitle("Active riders (in logs)", height(5)) title(" ")
		xtitle(Weeks)  xlabel(-30[10]70) ylabel(-.4[.4]1)  yline(0, lcolor(black))
		legend(off)  xline(0, lcolor(cranberry) lwidth(thick))
		graphregion(color(white)) plotregion(fcolor(white));
		#delimit cr	
		graph export "$dirout/Figure2c.eps", replace
restore	

xtscc ln_su tte_1-tte_46 tte_48-tte_125 time_2-time_87 e_rate inc_pc trend trend2 precipitation ln_tourism, fe

preserve
	matrix results = [e(b)',vecdiag(e(V))']
		svmat results
		keep results* 
		drop if results1==.
		gen n=_n
		keep if n<125
		gen event=-48
		replace event = even+n
		replace event=event+1 if event>=-1
		set obs 125
		replace results1 = 0 in 125
		replace results2 = 0 in 125
		replace event = -1 in 125
		drop n
		sort event
		rename results1 variable
		cap drop c1 c2
		gen c1=variable + 1.96*sqrt(results2)
		gen c2=variable - 1.96*sqrt(results2)

		#delimit
		twoway 
		(rarea c1 c2 event if event>=-35 & event<=70, pstyle(ci) color(gs15))
		(line variable event if event>=-35 & event<=70, lwidth(thick) lcolor(navy)) 
		, ytitle("Rider sign up (in logs)", height(5)) title(" ")
		xtitle(Weeks)  xlabel(-30[10]70) ylabel(-.4[.4]1.6)  yline(0, lcolor(black))
		legend(off)  xline(0, lcolor(cranberry) lwidth(thick))
		graphregion(color(white)) plotregion(fcolor(white));
		#delimit cr	
		graph export "$dirout/Figure2d.eps", replace
restore	



xtscc ln_driver_hours tte_1-tte_46 tte_48-tte_125 time_2-time_87 e_rate inc_pc trend trend2 precipitation ln_tourism, fe

preserve
	matrix results = [e(b)',vecdiag(e(V))']
		svmat results
		keep results* 
		drop if results1==.
		gen n=_n
		keep if n<125
		gen event=-48
		replace event = even+n
		replace event=event+1 if event>=-1
		set obs 125
		replace results1 = 0 in 125
		replace results2 = 0 in 125
		replace event = -1 in 125
		drop n
		sort event
		rename results1 variable
		cap drop c1 c2
		gen c1=variable + 1.96*sqrt(results2)
		gen c2=variable - 1.96*sqrt(results2)

		#delimit
		twoway 
		(rarea c1 c2 event if event>=-35 & event<=70, pstyle(ci) color(gs15))
		(line variable event if event>=-35 & event<=70, lwidth(thick) lcolor(navy)) 
		, ytitle("Driver hours (weekly, in logs)", height(5)) title(" ")
		xtitle(Weeks)  xlabel(-30[10]70) ylabel(-.1[.1].3)  yline(0, lcolor(black))
		legend(off)  xline(0, lcolor(cranberry) lwidth(thick))
		graphregion(color(white)) plotregion(fcolor(white));
		#delimit cr	
		graph export "$dirout/Figure2e.eps", replace
restore	


xtscc ln_driver_su tte_1-tte_46 tte_48-tte_125 time_2-time_87 e_rate inc_pc trend trend2 precipitation ln_tourism, fe
preserve
	matrix results = [e(b)',vecdiag(e(V))']
		svmat results
		keep results* 
		drop if results1==.
		gen n=_n
		keep if n<125
		gen event=-48
		replace event = even+n
		replace event=event+1 if event>=-1
		set obs 125
		replace results1 = 0 in 125
		replace results2 = 0 in 125
		replace event = -1 in 125
		drop n
		sort event
		rename results1 variable
		cap drop c1 c2
		gen c1=variable + 1.96*sqrt(results2)
		gen c2=variable - 1.96*sqrt(results2)

		#delimit
		twoway 
		(rarea c1 c2 event if event>=-35 & event<=70, pstyle(ci) color(gs15))
		(line variable event if event>=-35 & event<=70, lwidth(thick) lcolor(navy)) 
		, ytitle("Driver sign up (in logs)", height(5)) title(" ")
		xtitle(Weeks)  xlabel(-30[10]70) ylabel(-.8[.4]1.2)  yline(0, lcolor(black))
		legend(off)  xline(0, lcolor(cranberry) lwidth(thick))
		graphregion(color(white)) plotregion(fcolor(white));
		#delimit cr	
		graph export "$dirout/Figure2f.eps", replace
restore	


xtscc ln_active_riders_drivers tte_1-tte_46 tte_48-tte_125 time_2-time_87 e_rate inc_pc trend trend2 precipitation ln_tourism, fe

preserve
	matrix results = [e(b)',vecdiag(e(V))']
		svmat results
		keep results* 
		drop if results1==.
		gen n=_n
		keep if n<125
		gen event=-48
		replace event = even+n
		replace event=event+1 if event>=-1
		set obs 125
		replace results1 = 0 in 125
		replace results2 = 0 in 125
		replace event = -1 in 125
		drop n
		sort event
		rename results1 variable
		cap drop c1 c2
		gen c1=variable + 1.96*sqrt(results2)
		gen c2=variable - 1.96*sqrt(results2)

		#delimit
		twoway 
		(rarea c1 c2 event if event>=-35 & event<=70, pstyle(ci) color(gs15))
		(line variable event if event>=-35 & event<=70, lwidth(thick) lcolor(navy)) 
		, ytitle("Active riders over active drivers (in logs)", height(5)) title(" ")
		xtitle(Weeks)  xlabel(-30[10]70) ylabel(-.2[.2].4)  yline(0, lcolor(black))
		legend(off)  xline(0, lcolor(cranberry) lwidth(thick))
		graphregion(color(white)) plotregion(fcolor(white));
		#delimit cr	
		graph export "$dirout/Figure3a.eps", replace
restore	



xtscc fares_per_activedriver tte_1-tte_46 tte_48-tte_125 time_2-time_87 e_rate inc_pc trend trend2 precipitation ln_tourism, fe

		
preserve
	matrix results = [e(b)',vecdiag(e(V))']
		svmat results
		keep results* 
		drop if results1==.
		gen n=_n
		keep if n<125
		gen event=-48
		replace event = even+n
		replace event=event+1 if event>=-1
		set obs 125
		replace results1 = 0 in 125
		replace results2 = 0 in 125
		replace event = -1 in 125
		drop n
		sort event
		rename results1 variable
		cap drop c1 c2
		gen c1=variable + 1.96*sqrt(results2)
		gen c2=variable - 1.96*sqrt(results2)
		
		#delimit
		twoway 
		(rarea c1 c2 event if event>=-35 & event<=70, pstyle(ci) color(gs15))
		(line variable event if event>=-35 & event<=70, lwidth(thick) lcolor(navy)) 
		, ytitle("Fares per active driver (dollars)", height(5)) title(" ")
		xtitle(Weeks)  xlabel(-30[10]70) ylabel(-30[20]60)  yline(0, lcolor(black))
		legend(off)  xline(0, lcolor(cranberry) lwidth(thick))
		graphregion(color(white)) plotregion(fcolor(white));
		#delimit cr	
		graph export "$dirout/Figure3b.eps", replace
restore	
	
xtscc ln_epaid_trips tte_1-tte_46 tte_48-tte_125 time_2-time_87 e_rate inc_pc trend trend2 precipitation ln_tourism, fe

preserve
	matrix results = [e(b)',vecdiag(e(V))']
		svmat results
		keep results* 
		drop if results1==.
		gen n=_n
		keep if n<125
		gen event=-48
		replace event = even+n
		replace event=event+1 if event>=-1
		set obs 125
		replace results1 = 0 in 125
		replace results2 = 0 in 125
		replace event = -1 in 125
		drop n
		sort event
		rename results1 variable
		cap drop c1 c2
		gen c1=variable + 1.96*sqrt(results2)
		gen c2=variable - 1.96*sqrt(results2)

		#delimit
		twoway 
		(rarea c1 c2 event if event>=-35 & event<=70, pstyle(ci) color(gs15))
		(line variable event if event>=-35 & event<=70, lwidth(thick) lcolor(navy)) 
		, ytitle("Trips in card (in logs)", height(5)) title(" ")
		xtitle(Weeks)  xlabel(-30[10]70) ylabel(-.4[.2].4)  yline(0, lcolor(black))
		legend(off)  xline(0, lcolor(cranberry) lwidth(thick))
		graphregion(color(white)) plotregion(fcolor(white));
		#delimit cr	
		graph export "$dirout/Figure3c.eps", replace
restore	

xtscc avg_eta_min tte_1-tte_46 tte_48-tte_125 time_2-time_87 e_rate inc_pc trend trend2 precipitation ln_tourism, fe

preserve
	matrix results = [e(b)',vecdiag(e(V))']
		svmat results
		keep results* 
		drop if results1==.
		gen n=_n
		keep if n<125
		gen event=-48
		replace event = even+n
		replace event=event+1 if event>=-1
		set obs 125
		replace results1 = 0 in 125
		replace results2 = 0 in 125
		replace event = -1 in 125
		drop n
		sort event
		rename results1 variable
		cap drop c1 c2
		gen c1=variable + 1.96*sqrt(results2)
		gen c2=variable - 1.96*sqrt(results2)

		#delimit
		twoway 
		(rarea c1 c2 event if event>=-35 & event<=70, pstyle(ci) color(gs15))
		(line variable event if event>=-35 & event<=70, lwidth(thick) lcolor(navy)) 
		, ytitle("Avg. ETA (minutes)", height(5)) title(" ")
		xtitle(Weeks)  xlabel(-30[10]70) ylabel(-4[2]2)  yline(0, lcolor(black))
		legend(off)  xline(0, lcolor(cranberry) lwidth(thick))
		graphregion(color(white)) plotregion(fcolor(white));
		#delimit cr	
		graph export "$dirout/Figure3f.eps", replace
restore		


xtscc ln_fares_hours tte_1-tte_46 tte_48-tte_125 time_2-time_87 e_rate inc_pc trend trend2 precipitation ln_tourism, fe

preserve
	matrix results = [e(b)',vecdiag(e(V))']
		svmat results
		keep results* 
		drop if results1==.
		gen n=_n
		keep if n<125
		gen event=-48
		replace event = even+n
		replace event=event+1 if event>=-1
		set obs 125
		replace results1 = 0 in 125
		replace results2 = 0 in 125
		replace event = -1 in 125
		drop n
		sort event
		rename results1 variable
		cap drop c1 c2
		gen c1=variable + 1.96*sqrt(results2)
		gen c2=variable - 1.96*sqrt(results2)

		#delimit
		twoway 
		(rarea c1 c2 event if event>=-35 & event<=70, pstyle(ci) color(gs15))
		(line variable event if event>=-35 & event<=70, lwidth(thick) lcolor(navy)) 
		, ytitle("Fares per hour (in logs)", height(5)) title(" ")
		xtitle(Weeks)  xlabel(-30[10]70) ylabel(-.4[.2].6)  yline(0, lcolor(black))
		legend(off)  xline(0, lcolor(cranberry) lwidth(thick))
		graphregion(color(white)) plotregion(fcolor(white));
		#delimit cr	
		graph export "$dirout/FigureA2b.eps", replace
restore	


xtscc cancel_rate tte_1-tte_46 tte_48-tte_125 time_2-time_87 e_rate inc_pc trend trend2 precipitation ln_tourism, fe		
 			
preserve
	matrix results = [e(b)',vecdiag(e(V))']
		svmat results
		keep results* 
		drop if results1==.
		gen n=_n
		keep if n<125
		gen event=-48
		replace event = even+n
		replace event=event+1 if event>=-1
		set obs 125
		replace results1 = 0 in 125
		replace results2 = 0 in 125
		replace event = -1 in 125
		drop n
		sort event
		rename results1 variable
		cap drop c1 c2
		gen c1=variable + 1.96*sqrt(results2)
		gen c2=variable - 1.96*sqrt(results2)		
		
		#delimit
		twoway 
		(rarea c1 c2 event if event>=-35 & event<=70, pstyle(ci) color(gs15))
		(line variable event if event>=-35 & event<=70, lwidth(thick) lcolor(navy)) 
		, ytitle("Cancellation rate", height(5)) title(" ")
		xtitle(Weeks) xlabel(-30[10]70)  ylabel(-0.01[.01]0.03)  yline(0, lcolor(black))
		legend(off)  xline(0, lcolor(cranberry) lwidth(thick))
		graphregion(color(white)) plotregion(fcolor(white));
		#delimit cr	
		
		graph export "$dirout/FigureA1a.eps", replace
restore	
		

xtscc ln_active_drivers tte_1-tte_46 tte_48-tte_125 time_2-time_87 e_rate inc_pc trend trend2 precipitation ln_tourism, fe

preserve
	matrix results = [e(b)',vecdiag(e(V))']
		svmat results
		keep results* 
		drop if results1==.
		gen n=_n
		keep if n<125
		gen event=-48
		replace event = even+n
		replace event=event+1 if event>=-1
		set obs 125
		replace results1 = 0 in 125
		replace results2 = 0 in 125
		replace event = -1 in 125
		drop n
		sort event
		rename results1 variable
		cap drop c1 c2
		gen c1=variable + 1.96*sqrt(results2)
		gen c2=variable - 1.96*sqrt(results2)

		#delimit
		twoway 
		(rarea c1 c2 event if event>=-35 & event<=70, pstyle(ci) color(gs15))
		(line variable event if event>=-35 & event<=70, lwidth(thick) lcolor(navy)) 
		, ytitle("Active drivers (in logs)", height(5)) title(" ")
		xtitle(Weeks)  xlabel(-30[10]70) ylabel(-.4[.4].8)  yline(0, lcolor(black))
		legend(off)  xline(0, lcolor(cranberry) lwidth(thick))
		graphregion(color(white)) plotregion(fcolor(white));
		#delimit cr	
		graph export "$dirout/FigureA1b.eps", replace
restore	
		
		


global dirin = " "
cd " "

use "launch_dates_clean.dta", clear
gen intro_date=ym(intro_year,intro_month)
keep city same intro_date
rename city city_name
set obs 45
replace city_name = "Queretaro" in 44
replace same = 0 in 44
replace city_name = "Estado de Mexico" in 45
replace same = 0 in 45
replace intro_date=682 in 45

cd "$dirin"
save "TEMP_launch_dates.dta", replace

use INPC_transport_gasoline.dta, clear
rename city city_name
merge m:1 city_name using "TEMP_launch_dates.dta", nogenerate
erase "TEMP_launch_dates.dta"
format intro_date %tm
rename city_name city

merge m:1 city year month using ciudad_estado_mensual_short
drop if _merge==2
drop _merge

keep if all_dates>=658
keep if all_dates<702 


egen city_id=group(city)
tsset city_id all_dates

gen time_to_event=(all_dates-intro_date)
gen EVENT=(time_to_event==0)

bysort city_id: egen min_time_to_event=min(time_to_event)
tab min_time_to_event
drop if intro_date==. & city!="Queretaro"


drop if min_time_to_event>-2 & city!="Queretaro"
drop min_time_to_event
drop if time_to_event==. & city!="Queretaro"
bysort city_id: egen max_time_to_event=max(time_to_event)


tab time_to_event, g(tte_)
forvalues i=1/53 {
replace tte_`i'=0 if city=="Queretaro"
}

sort city_id all_dates
egen time=group(all_dates)
replace inc_pc=ln(inc_pc)

tab time, g(time_)
tsset city_id time

xtscc log_inflationtaxi tte_1-tte_27 tte_29-tte_53 time_2-time_44 e_rate inc_pc log_inflationgasoline, fe
	matrix results = [e(b)',vecdiag(e(V))']
		svmat results
		keep results* 
		drop if results1==.
		gen n=_n
		keep if n<53
		gen event=-29
		replace event = even+n
		replace event=event+1 if event>=-1
		set obs 53
		replace results1 = 0 in 53
		replace results2 = 0 in 53
		replace event = -1 in 53
		drop n
		sort event
		rename results1 variable
		cap drop c1 c2
		gen c1=variable + 1.96*sqrt(results2)
		gen c2=variable - 1.96*sqrt(results2)

		#delimit
		twoway 
		(rarea c1 c2 event if event>=-12 & event<=18, pstyle(ci) color(gs15))
		(line variable event if event>=-12 & event<=18, lwidth(thick) lcolor(navy)) 
		, ytitle("Price (in logs)", height(5)) title(" ")
		xtitle(Months) xlabel(-12[6]18) ylabel(-.08[.04]0.08)  yline(0, lcolor(black))
		legend(off)  xline(0, lcolor(cranberry) lwidth(thick))
		graphregion(color(white)) plotregion(fcolor(white));
		#delimit cr	
		graph export "$dirout/FigureA2a.eps", replace


